package apy.codecase.greendao;

import android.content.Context;
import android.util.Log;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

import apy.codecase.greendao.gen.DiaryDaoModelDao;
import apy.codecase.model.DiaryDaoModel;

/**
 * Created by Administrator on 2017/10/21.
 */

public class DiaryDaoUtils {
    private static final String TAG = DiaryDaoUtils.class.getSimpleName();
    private DaoManager mManager;

    public DiaryDaoUtils(Context context) {
        mManager = DaoManager.getInstance();
        mManager.init(context);
    }

    /**
     * 完成DiaryDaoModel记录的插入，如果表未创建，先创建DiaryDaoModel表
     *
     * @param DiaryDaoModel
     * @return
     */
    public boolean insertDiaryDaoModel(DiaryDaoModel DiaryDaoModel) {
        boolean flag = false;
        flag = mManager.getDaoSession().getDiaryDaoModelDao().insert(DiaryDaoModel) == -1 ? false : true;
        Log.i(TAG, "insert DiaryDaoModel :" + flag + "-->" + DiaryDaoModel.toString());
        return flag;
    }

    /**
     * 插入多条数据，在子线程操作
     *
     * @param DiaryDaoModelList
     * @return
     */
    public boolean insertMultDiaryDaoModel(final List<DiaryDaoModel> DiaryDaoModelList) {
        boolean flag = false;
        try {
            mManager.getDaoSession().runInTx(new Runnable() {
                @Override
                public void run() {
                    for (DiaryDaoModel DiaryDaoModel : DiaryDaoModelList) {
                        mManager.getDaoSession().insertOrReplace(DiaryDaoModel);
                    }
                }
            });
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 修改一条数据
     *
     * @param DiaryDaoModel
     * @return
     */
    public boolean updateDiaryDaoModel(DiaryDaoModel DiaryDaoModel) {
        boolean flag = false;
        try {
            mManager.getDaoSession().update(DiaryDaoModel);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除单条记录
     *
     * @param DiaryDaoModel
     * @return
     */
    public boolean deleteDiaryDaoModel(DiaryDaoModel DiaryDaoModel) {
        boolean flag = false;
        try {
            //按照id删除
            mManager.getDaoSession().delete(DiaryDaoModel);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除所有记录
     *
     * @return
     */
    public boolean deleteAll() {
        boolean flag = false;
        try {
            //按照id删除
            mManager.getDaoSession().deleteAll(DiaryDaoModel.class);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 查询所有记录
     *
     * @return
     */
    public List<DiaryDaoModel> queryAllDiaryDaoModel() {
        return mManager.getDaoSession().loadAll(DiaryDaoModel.class);
    }

    /**
     * 根据主键id查询记录
     *
     * @param key
     * @return
     */
    public DiaryDaoModel queryDiaryDaoModelById(long key) {
        return mManager.getDaoSession().load(DiaryDaoModel.class, key);
    }

    /**
     * 使用native sql进行查询操作
     */
    public List<DiaryDaoModel> queryDiaryDaoModelByNativeSql(String sql, String[] conditions) {
        return mManager.getDaoSession().queryRaw(DiaryDaoModel.class, sql, conditions);
    }

    /**
     * 使用queryBuilder进行查询
     *
     * @return
     */
    public List<DiaryDaoModel> queryDiaryDaoModelByQueryBuilder(long id) {
        QueryBuilder<DiaryDaoModel> queryBuilder = mManager.getDaoSession().queryBuilder(DiaryDaoModel.class);
        return queryBuilder.where(DiaryDaoModelDao.Properties._id.eq(id)).list();
    }

    public List<DiaryDaoModel> queryDiaryDaoModelByQueryBuilderID(String id) {
        QueryBuilder<DiaryDaoModel> queryBuilder = mManager.getDaoSession().queryBuilder(DiaryDaoModel.class);
        return queryBuilder.where(DiaryDaoModelDao.Properties.Id.eq(id)).list();
    }
}
